#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: JeoKayne ATaier
import tkinter as tk
import random
import string
import tkinter.messagebox
from tkinter import scrolledtext
import os
from icon import img
import base64
import threading

window = tk.Tk()
window.title(" CDK生成器 - Ataier ")
window.geometry("315x560")
window.resizable(False,False)


# 图标配置
tmp = open("tmp.ico", "wb+")
tmp.write(base64.b64decode(img))
tmp.close()
window.iconbitmap("tmp.ico")
os.remove("tmp.ico")

# 生成CDK
def generate_cdk(cdk_lenth, cdk_initial):
    '''
    string.digits = 0123456789
    string.ascii_letters = 26个小写,26个大写
    '''
    cdk_lenth = int(cdk_lenth) - len(cdk_initial)
    str_list = random.sample(string.digits + string.ascii_letters, cdk_lenth)
    random_str = ''.join(str_list)
    return random_str
    pass


# 物品ID和物品数量
def iteminfo(itemid, itemnum):
    iteminfo = itemid + "\t" + itemnum
    return iteminfo


# 写入txt文件并保存
def write_file(filedir, cdk, iteminfo):
    # 拼接CDK+item并写入
    with open(filedir, "a+") as w:
        info = cdk + "\t" + iteminfo
        w.write(info + "\n")


# 打乱CDK顺序
def chaos_cdk(dir):
    with open(dir, "r") as f:
        cdk_list = f.readlines()
        random.shuffle(cdk_list)
    with open(dir, "w+") as r:
        r.write("")
    for i in cdk_list:
        with open(dir, "a") as s:
            s.write(i)
            # print(i)


if __name__ == '__main__':




    # 生成左侧标签并显示
    label_text = tk.Label(window, text="注意：生成数量大时,运行速度较慢", fg="red")
    label_text.pack()
    l_innitial = tk.Label(window, text="CDK前缀")
    l_innitial.place(x=40, y=30)
    l_lenth = tk.Label(window, text="CDK长度")
    l_lenth.place(x=40, y=55)
    l_num = tk.Label(window, text="CDK数量")
    l_num.place(x=40, y=80)
    l_id = tk.Label(window, text="物品编号")
    l_id.place(x=40, y=105)
    l_idnum = tk.Label(window, text="物品数量")
    l_idnum.place(x=40, y=130)

    # 生成信息输入框
    e1 = tk.Entry(window)
    e2 = tk.Entry(window)
    e3 = tk.Entry(window)
    e4 = tk.Entry(window)
    e5 = tk.Entry(window)
    e1.place(x=100, y=30)
    e2.place(x=100, y=55)
    e3.place(x=100, y=80)
    e4.place(x=100, y=105)
    e5.place(x=100, y=130)

    # 生成文本输出框

    # text_out = tk.Text(window,width=42)
    # text_out.place(x=9,y=220)
    dc = scrolledtext.ScrolledText(window, width=38)
    dc.place(x=18, y=220)

    # 生成CDK打乱勾选项
    var_checkb = tk.IntVar()
    c = tk.Checkbutton(window, text="打乱CDK顺序", variable=var_checkb, onvalue=1)
    choice = var_checkb.get()
    c.place(x=110, y=150)


    # 写出上述数据处理命令，供生成按钮使用
    def generator():
        try:
            cdk_initial = e1.get()
            cdk_lenth = e2.get()
            cdk_num = e3.get()
            itemid = e4.get()
            itemnum = e5.get()
            choice = var_checkb.get()
            cdk_num = int(cdk_num)
            item_info = iteminfo(itemid, itemnum)
            if len(cdk_initial) >= int(cdk_lenth):
                exit()
            # 检测文件是否存在

            if not os.path.exists("./cdk.txt"):
                with open("./cdk.txt","w") as f:
                    f.write("")
            for _ in range(cdk_num):
                cdk = cdk_initial + generate_cdk(cdk_lenth, cdk_initial)
                dc.insert("end", cdk + "\t" + item_info + "\n")
                dc.see("end")
                write_file("./cdk.txt", cdk, item_info)
            # 是否打乱顺序
            if choice == 1:
                chaos_cdk("./cdk.txt")
            tk.messagebox.showinfo(title="提示", message="生成完毕！")
        except:
            # print(error)
            tk.messagebox.showerror(title="提示", message="生成失败！请输入正确信息！")


    # 生成按钮
    b = tk.Button(window, text="生成", width=15, command=generator)
    b.place(x=110, y=185)


    # 生成关于菜单

    def aboutme():
        tk.messagebox.askyesno(title="关于", message="JeoKayne Ataier\n作者:阿泰尔 \nQQ:3284809656 \n完全免费 禁止倒卖")


    def helpme():
        tk.messagebox.askquestion(title="帮助", message="正确填写信息并点击生成，cdk.txt文件将自动保存于本软件同目录下")


    menubar = tk.Menu(window)
    aboutmenu = tk.Menu(menubar, tearoff=0)
    menubar.add_cascade(label="关于", menu=aboutmenu)
    aboutmenu.add_command(label="关于作者", command=aboutme)
    aboutmenu.add_command(label="使用帮助", command=helpme)
    aboutmenu.add_separator()
    aboutmenu.add_command(label="退出", command=window.quit)
    window.config(menu=menubar)

    # 窗口配置及显示循环

    window.mainloop()
